﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.DAL.RecordCenter;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.BLL.BackdropForm
{
    public class BackdropFormBLL
    {
        public static void AddBackDrop(string title, string code, double? version, FondosFondo backdropParent)
        {
            //FondosFondo element = null;
            //using (DataContext ctx = new DataContext(SPContext.Current.Web.Url))
            //{
            //    element = new FondosFondo
            //    {
            //        Código = code,
            //        Title = title,
            //        VersiónTablaDeRetención = version,
            //        FondoAlQuePertenece = backdropParent
            //    };
            //    if (backdropParent != null) { ctx.Fondos.Attach(backdropParent); }
            //    ctx.Fondos.InsertOnSubmit(element);

            //    ctx.SubmitChanges();
            //}
            SPContext.Current.Web.AllowUnsafeUpdates = true;
            SPList targetList = SPContext.Current.Web.Lists.TryGetList(GlobalConstants.LIST_BIBLIOTECA_FONDO);
            SPListItem item = targetList.Items.Add();

            item[SPBuiltInFieldId.Title] = title;
            item[CustomSiteColumns.FIELD_CODE] = code;
            item[CustomSiteColumns.FIELD_BACKDROP_VERSION] = version;
            item[CustomSiteColumns.FIELD_BACKDROP_PARENT] = backdropParent == null ? null : new SPFieldLookupValue { LookupId = backdropParent.Id.Value };
            
            item.Update();
            SPContext.Current.Web.AllowUnsafeUpdates = false;
        }

        public static void UpdateBackDrop(int id, string title, string code, double? version, FondosFondo backdropParent)
        {
            //FondosFondo element = null;
            //using (DataContext ctx = new DataContext(SPContext.Current.Web.Url))
            //{
            //    element = ctx.Fondos.SingleOrDefault(backdrop => backdrop.Id.Equals(id));
            //    if (element == default(FondosFondo)) { throw new Exception(string.Format("No existe un fondo con el id {0}", id)); }

            //    if (backdropParent != null) { ctx.Fondos.Attach(backdropParent); }

            //    element.Title = title;
            //    element.Código = code;
            //    element.VersiónTablaDeRetención = version;
            //    element.FondoAlQuePertenece = backdropParent;

            //    ctx.SubmitChanges();
            //}
            SPContext.Current.Web.AllowUnsafeUpdates = true;
            SPList targetList = SPContext.Current.Web.Lists.TryGetList(GlobalConstants.LIST_BIBLIOTECA_FONDO);
            SPListItem item = targetList.GetItemById(id);

            item[SPBuiltInFieldId.Title] = title;
            item[CustomSiteColumns.FIELD_CODE] = code;
            item[CustomSiteColumns.FIELD_BACKDROP_VERSION] = version;
            item[CustomSiteColumns.FIELD_BACKDROP_PARENT] = backdropParent == null ? null : new SPFieldLookupValue { LookupId = backdropParent.Id.Value };

            item.Update();
            SPContext.Current.Web.AllowUnsafeUpdates = false;

        }

        public static FondosFondo GetBackdrop(int id)
        {
            FondosFondo element = null;
            using (DataContext ctx = new DataContext(SPContext.Current.Web.Url))
            {
                element = ctx.Fondos.SingleOrDefault(backdrop => backdrop.Id.Equals(id));
            }
            if (element == default(FondosFondo)) { throw new Exception(string.Format("No existe un fondo con el id {0}", id)); }
            return element;
        }

        public static bool PrimaryBackdropAlreadyExists()
        {
            bool exists = false;
            using (DataContext ctx = new DataContext(SPContext.Current.Web.Url))
            {

                exists = ctx.Fondos.ToList().Count(backdrop => backdrop.FondoAlQuePertenece == null) > 0;
            }
            return exists;
        }

        public static bool PrimaryBackdropAlreadyExists(int id)
        {
            bool exists = false;
            using (DataContext ctx = new DataContext(SPContext.Current.Web.Url))
            {

                exists = ctx.Fondos.Where(backdrop => backdrop.Id != id).ToList().Count(backdrop => backdrop.FondoAlQuePertenece == null) > 0;
            }
            return exists;
        }

        public static IEnumerable<FondosFondo> GetBackDrops()
        {

            IEnumerable<FondosFondo> entities = null;
            using (DataContext ctx = new DataContext(SPContext.Current.Web.Url))
            {
                entities = ctx.Fondos.OrderBy(fondo => fondo.Title);
            }

            return entities;
        }
    }
}
